<template>
  <div style="display: inline">
    <el-tooltip class="item" effect="dark" content="同一个商品可能用于不同的站点，通过「编辑适用ID」可关联不同站点的同一商品，方便库存的使用" placement="top">
      <el-button type="danger" @click="showDiaLog">编辑适用ID</el-button>
    </el-tooltip>

    <el-dialog
      class="select-goods-dialog"
      title="添加其他适用的Listing ID和SKU"
      :visible.sync="dialogSuit"
      :modal-append-to-body="false"
    >
      <div v-loading="loading">
        <div class="select-goods">
          当前选择的商品
          <el-table
            ref="selectGoods"
            :data="selectGoods"
            :border="true"
            row-key="id"
            class="goods-table"
          >
            <el-table-column
              prop="image"
              label="商品"
              align="left"
              width="120"
            >
              <template slot-scope="{row}">
                <img :src="row.image" alt="" width="70" style="margin-left: 20px">
              </template>
            </el-table-column>
            <el-table-column
              prop="title"
              label="标题"
              align="left"
              min-width="250"
            >
              <template slot-scope="{row}">
                <el-tooltip class="item" effect="dark" :content="row.title" placement="top">
                  <div class="hidden-2">
                    {{row.title}}
                  </div>
                </el-tooltip>
                <!-- 如果是变体则直接显示对应的属性 -->
                <template v-if="row.attributes">
                  <div class="goods-SKU-info">
                <span
                  v-for="attr in row.attributes"
                  :key="attr.name"
                  class="attr"
                >
                  {{attr.name}} : {{attr.value}}
                  <span class="attr-link"> | </span>
                </span>
                  </div>
                </template>

              </template>
            </el-table-column>
            <el-table-column
              label="Listing ID与SKU"
              align="center"
              min-width="150"
            >
              <template slot-scope="{row}">
                <div>{{row.goodsId}} {{row.sku ? `(${row.sku})` : ''}}</div>
              </template>
            </el-table-column>
            <el-table-column
              label="操作"
              align="center"
              min-width="100"
            >
              <template slot-scope="{row}">
                <diy-link @click="removeSelect(row)" v-if="selectGoods.length > 1">移除</diy-link>
              </template>
            </el-table-column>
          </el-table>
        </div>

        <p>同一商品可能用于多个不同的站点，请通过搜索该商品在其他站点的Listing ID 或 SKU ，勾选后完成绑定</p>

        <div style="margin: 10px 0;">
          <el-input size="small" style="width: 200px" v-model="suitParams.keyword" placeholder="搜索 listing或sku"></el-input>
          <el-button size="small" type="primary" @click="getSuitData">搜索</el-button>
        </div>
        <el-table
          ref="goodsTable"
          :data="suitData"
          :border="true"
          row-key="id"
          v-loading="goodsTableLoading"
          :tree-props="{children: 'goodsVariation'}"
          class="goods-table"
          default-expand-all
        >
          <el-table-column
            prop="image"
            label="商品"
            align="left"
            width="120"
          >
            <template slot-scope="{row}">
              <img :src="row.image" alt="" width="70" style="margin-left: 20px">
            </template>
          </el-table-column>
          <el-table-column
            prop="title"
            label="标题"
            align="left"
            min-width="250"
          >
            <template slot-scope="{row}">
              <!-- 如果是变体则直接显示对应的属性 -->
              <template v-if="row.variationId">
                <div class="goods-SKU-info">
                <span
                  v-for="attr in row.attributes"
                  :key="attr.name"
                  class="attr"
                >
                  {{attr.name}} : {{attr.value}}
                  <span class="attr-link"> | </span>
                </span>
                </div>


              </template>
              <template v-else>
                <el-tooltip class="item" effect="dark" :content="row.title" placement="top">
                  <div class="hidden-2">
                    {{row.title}}
                  </div>
                </el-tooltip>
              </template>

            </template>
          </el-table-column>
          <el-table-column
            label="Listing ID与SKU"
            align="center"
            min-width="150"
          >
            <template slot-scope="{row}">
              <div>{{row.goodsId}} {{row.sku ? `(${row.sku})` : ''}}</div>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            min-width="100"
          >
            <template slot-scope="{row}">
              <el-link :underline="false" @click="addSelectGoods(row)" :disabled="disAbleAdd(row)" v-if="!row.isVariation">添加</el-link>
            </template>
          </el-table-column>
        </el-table>
        <div class="footer">
          <el-button @click="dialogSuit = false">取消</el-button>
          <el-button @click="confirmSuit" :loading="suitConfirmLoading" type="primary">确认</el-button>
        </div>

      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getGoodsList, getGoodsSuit, saveGoodsSuit} from "@/api/goods";
import {deepCopy} from "@/utils";

export default {
  name: "suitGoods",
  watch:{
    dialogSuit(val){
      if(val){
        this.getData();
      }
    }
  },
  props:{
    suitId: {
      type: Number,
      required: true,
    }
  },
  data(){
    return {
      dialogSuit: false,
      loading: false, // 整个dialog的loading
      selectGoods: [], // 当前适用的商品
      suitParams:{
        keyword: '',
        selectType: 2,
      },
      suitData: [],
      goodsTableLoading: false, // 商品搜索动画
      suitConfirmLoading: false, // 表单提交操作

    };
  },
  methods:{
    // 获取适用的商品列表
    getData(){
      this.loading = true
      getGoodsSuit(this.suitId)
        .then(res=>{
          this.loading = false
          this.selectGoods = res.data
        })
    },
    showDiaLog(){
      this.dialogSuit = true
    },
    // 移除关联商品
    removeSelect(row){
      this.selectGoods = this.selectGoods.filter(item => item.id !== row.id)
    },
    // 搜索商品
    getSuitData(){
      this.goodsTableLoading = true
      getGoodsList(this.suitParams)
        .then(res=>{
          this.goodsTableLoading = false
          const { data, info } = res
          this.suitData = deepCopy(data)

        })
    },
    // 添加关联商品
    addSelectGoods(row) {
      this.selectGoods.push(row)
    },
    // 判断是否能添加到关联商品
    disAbleAdd(row){
      if(!row.suitId){
        return true;
      }

      let flag = this.selectGoods.find(item=>{
        if(item.id === row.id){
          return true;
        }
      })
      return !!flag;
    },
    // 提交关联商品表单
    confirmSuit(){
      this.suitConfirmLoading = true
      const postData = {
        suitId: this.suitId,
        suitGoods: []
      }
      this.selectGoods.forEach(item=>{
        postData.suitGoods.push({
          goodsId: item.goodsId,
          variationId: item.variationId,
        })
      })
      saveGoodsSuit(postData)
        .then(res=>{
          this.suitConfirmLoading = false
          if(res.error === 0){
            this.$successMessage(res.message)
            this.$emit('changeGoodsSuit',true); // 通知父组件 表单有变化
            this.dialogSuit = false
          }else{
            this.$errorMessage(res.message)
          }
        })
    },


  }
}
</script>

<style lang="scss">
.goods-table{
  .el-table__expand-icon {
    float: left;
    line-height: 100px;
    height: 100px;
    font-size: 14px;
    position: absolute;
    top: 30px;
    left: 5px;
  }
}
</style>

<style scoped lang="scss">
.channel-form{
  &.el-form{
    .el-form-item__label{
      word-break: break-word;
    }
  }
}
.channel-box-card{
  .el-card__header{
    padding: 5px 10px;
    font-size: 12px;
    line-height: 26px;
  }
  .el-card__body{
    padding: 0;
  }
}
.query-select{
  margin-right: 20px;
}
.select-goods-dialog{
  .footer{
    margin-top: 30px;
    display: flex;
    justify-content: flex-end;
  }
}
</style>
